from pyecharts import options as opts
from pyecharts.charts import Tree
from pyecharts.globals import ThemeType
from docx import Document
doc = Document('人工智能常见应用场景.docx')
data = []
title2 = []
title2_num, title2_temp = 0, 0
for para in doc.paragraphs:
    level = para.style.name
    if level == 'Heading 1':
        dict = {}
        dict['name'] = para.text
        data.append(dict)
    elif level == 'Heading 2':
        title2_num = title2_num + 1
        dict = {}
        dict['name'] = para.text.split('．')[1]
        title2.append(dict)
        data[0]['children'] = title2
    elif level == 'Heading 3':
        if title2_temp != title2_num:
           title2_temp = title2_num
           title3 = []
        dict = {}
        dict['name'] = para.text.split()[1]
        title3.append(dict)
        data[0]['children'][title2_num -1]['children'] = title3
tree = Tree(init_opts=opts.InitOpts(
    width='800px', height='900px',
    theme=ThemeType.LIGHT, page_title='树状图')
)
tree.add(series_name='', data=data,
         orient='LR',
         pos_top='5%',
         label_opts=opts.LabelOpts(
             position='top',
             distance=0,
             font_size='15',
         )
)
tree.set_global_opts(
    title_opts=opts.TitleOpts(
        title='人工智能常见应用场景树状图', pos_left='center'),
    legeng_opts=opts.LegendOpts(is_show=False)
)
tree.render('例8-2.html')